BASE_URL = "http://127.0.0.1:5030"
import requests, json
import matplotlib.pyplot as plt
def mat2list(mat):
    if hasattr(mat, '__len__'):
        return [mat2list(x) for x in mat]
    return int(mat)
def test_coords_transform_1():
    url = f"{BASE_URL}/task/coords_transform_1"
    data_0 = [0, 0]
    data_1 = json.loads(open("../../../rust/edp_files/system/homography.json", "r", encoding="utf-8").read())
    data = [data_0, data_1]
    response = requests.post(url, json=data)
    print(response.text)
def test_keypoint_selector():
    url = f"{BASE_URL}/task/keypoint_selector"
    data = [[[195.59586,145.08083],0.74045503],[[191.96074,138.67108],0.6506012],[[185.42267,145.88397],0.85906863],[[164.43591,145.42447],0.7214336]]
    # pointsx = [x[0][0] for x in data]
    # pointsy = [x[0][1] for x in data]
    # plt.scatter(pointsx, pointsy)
    # plt.show()
    response = requests.post(url, json=data)
    print(response.text)
def test_fbpoint_to_pose():
    url = f"{BASE_URL}/task/fbpoint_to_pose"
    data = [[[0.42, 0.234], [0,0], 0.8]]
    response = requests.post(url, json=data)
    print(response.text)
def test_mask_to_contour():
    url = f"{BASE_URL}/task/mask_to_contour"
    import cv2
    data = cv2.imread(r"M:\DLC-input\mask0.png")
    response = requests.post(url, json=mat2list(data))
    print(response.json())
def test_mask_to_contour_fin():
    url = f"{BASE_URL}/task/mask_to_contour_fin"
    data = r"M:\DLC-input\mask0.png"
    response = requests.post(url, json=data)
    print(response.text)
def test_contour_to_pose():
    url = f"{BASE_URL}/task/contour_to_pose"
    data = [[1154, 294], [1153, 295], [1152, 295], [1151, 296], [1149, 296], [1148, 297], [1144, 297], [1143, 298], [1140, 298], [1139, 299], [1138, 299], [1135, 302], [1134, 302], [1133, 303], [1132, 303], [1131, 304], [1129, 304], [1128, 305], [1127, 305], [1126, 306], [1124, 306], [1123, 307], [1119, 307], [1118, 308], [1116, 308], [1115, 309], [1108, 309], [1107, 310], [1104, 310], [1103, 311], [1098, 311], [1097, 312], [1096, 312], [1095, 313], [1093, 313], [1092, 314], [1090, 314], [1089, 315], [1086, 315], [1085, 316], [1082, 316], [1081, 317], [1078, 317], [1077, 318], [1077, 319], [1079, 321], [1078, 322], [1077, 322], [1074, 325], [1072, 325], [1071, 326], [1070, 326], [1069, 327], [1067, 327], [1065, 329], [1063, 329], [1061, 331], [1060, 331], [1058, 333], [1057, 333], [1055, 335], [1055, 336], [1052, 339], [1051, 339], [1047, 343], [1047, 344], [1046, 345], [1046, 346], [1045, 347], [1045, 364], [1046, 365], [1046, 367], [1047, 368], [1047, 369], [1049, 371], [1049, 372], [1050, 372], [1054, 376], [1054, 377], [1057, 380], [1061, 380], [1062, 381], [1074, 381], [1075, 382], [1100, 382], [1101, 381], [1104, 381], [1105, 380], [1114, 380], [1115, 379], [1116, 379], [1118, 377], [1119, 377], [1120, 376], [1121, 376], [1126, 371], [1126, 370], [1127, 369], [1128, 369], [1130, 367], [1131, 367], [1134, 364], [1135, 364], [1138, 361], [1139, 361], [1141, 359], [1144, 359], [1146, 357], [1147, 357], [1149, 355], [1150, 355], [1152, 353], [1153, 353], [1155, 351], [1156, 351], [1158, 349], [1159, 349], [1160, 348], [1161, 348], [1163, 346], [1164, 346], [1167, 343], [1167, 342], [1170, 339], [1172, 339], [1176, 335], [1177, 335], [1177, 334], [1180, 331], [1180, 330], [1181, 329], [1182, 329], [1182, 328], [1184, 326], [1184, 325], [1185, 324], [1185, 323], [1186, 322], [1186, 321], [1187, 320], [1187, 314], [1185, 312], [1185, 311], [1183, 309], [1182, 309], [1181, 308], [1180, 308], [1179, 307], [1178, 307], [1177, 306], [1177, 305], [1176, 305], [1172, 301], [1171, 301], [1168, 298], [1167, 298], [1164, 295], [1163, 295], [1162, 294]]
    # import cv2
    # import numpy as np
    # img = cv2.imread(r"M:\DLC-input\mask0.png")
    # cv2.drawContours(img, [np.array(data)], 0, (0, 255, 0), 2)
    # cv2.imshow("contour", img)
    response = requests.post(url, json=data)
    print(response.text)
def test_normalize_thetas():
    url = f"{BASE_URL}/task/normalize_thetas"
    data = [100, 101, 120, 140, 160, 170, -170, -160, -150, 0, 20, 40, 60, 80, 100, 120, 140, 160, 180, -160, 0]
    response = requests.post(url, json=data)
    print(response.text)
def test_stimulus_segment():
    url = f"{BASE_URL}/task/stimulus_segment"
    data = open(r"E:\Dev\files.GitHub\Cockroach-video-parse\out-light-every.txt").readlines()
    data = [int(x.strip()) for x in data]
    data = [[x, False] for x in data]
    response = requests.post(url, json=data)
    print(response.text)
if __name__ == "__main__":
    # test_coords_transform_0()
    # test_keypoint_selector()
    # test_fbpoint_to_pose()
    # test_mask_to_contour()
    # test_mask_to_contour_fin()
    # test_contour_to_pose()
    # test_normalize_thetas()
    # test_stimulus_segment()
    url = f"{BASE_URL}/task/stimulus_segment"
    data = json.load(open(r"C:\Users\songy\Desktop\data-0518\rstim.json", "r", encoding="utf-8"))
    data = [[x, True] for x in data]
    response = requests.post(url, json=data)
    print(response.text)
